﻿using IdentityServer4.Models;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dralee.IdentityServer.Host.Models;

namespace Dralee.IdentityServer.Host
{
    public class IdentityServerConfig
    {
        /// <summary>
        /// 定义 API资源
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiResource> GetApiResources(IdentityServerCfg cfg)
        {
            return new List<ApiResource>
            {
                new ApiResource(cfg.ApiName, cfg.ApiDisplayName)
                {
                    Description =cfg.ApiDescription,
                    ApiSecrets = { new Secret(cfg.ApiSecret) }
                }
            };
        }

        /// <summary>
        /// 定义身份资源
        /// </summary>
        /// <returns></returns>
        //public static IEnumerable<IdentityResource> GetIdentityResources()
        //{
        //    return new List<IdentityResource>
        //    {
        //        new IdentityResources.OpenId(),
        //        new IdentityResources.Profile(),
        //        new IdentityResources.Email(),
        //        new IdentityResources.Phone(),
        //        new IdentityResources.Address()
        //    };
        //}

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            var customProfile = new IdentityResource(
                name: "custom.profile",
                displayName: "Custom profile",
                claimTypes: new[] { "name", "email", "status" });

            return new List<IdentityResource>{
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                customProfile
            };
        }

        public static IEnumerable<Client> GetClients(IConfiguration configuration)
        {
            var clients = new List<Client>();

            foreach (var child in configuration.GetSection("IdentityServer:Clients").GetChildren())
            {
                clients.Add(new Client
                {
                    ClientId = child["ClientId"],
                    ClientName = child["ClientName"],
                    AllowedGrantTypes = child.GetSection("AllowedGrantTypes").GetChildren().Select(c => c.Value).ToArray(),
                    RequireConsent = bool.Parse(child["RequireConsent"] ?? "false"),
                    AllowOfflineAccess = bool.Parse(child["AllowOfflineAccess" ?? "false"]),
                    ClientSecrets = child.GetSection("ClientSecrets").GetChildren().Select(s => new Secret(s["Value"].Sha256())).ToArray(),
                    AllowedScopes = child.GetSection("AllowedScopes").GetChildren().Select(c => c.Value).ToArray(),
                    RedirectUris = child.GetSection("RedirectUris").GetChildren().Select(c => c.Value).ToArray(),
                    PostLogoutRedirectUris = child.GetSection("PostLogoutRedirectUris").GetChildren().Select(c => c.Value).ToArray()
                });
            }
            return clients;
        }

        public static IEnumerable<Client> GetClients(IdentityServerCfg identityServerCfg)
        {
            var clients = new List<Client>();

            foreach (var child in identityServerCfg.Clients)
            {
                clients.Add(new Client
                {
                    ClientId = child.ClientId,
                    ClientName = child.ClientName,
                    AllowedGrantTypes = child.AllowedGrantTypes,
                    RequireConsent = child.RequireConsent,
                    AllowOfflineAccess = child.AllowOfflineAccess,
                    ClientSecrets = child.ClientSecrets.Select(c => new Secret(c.Value.Sha256())).ToArray(),
                    AllowedScopes = child.AllowedScopes,
                    RedirectUris = child.RedirectUris,
                    PostLogoutRedirectUris = child.PostLogoutRedirectUris
                });
            }
            //clients.Add(new Client
            //{
            //    ClientId = "what",ClientName = "What the funck",
            //    AllowedGrantTypes =  new[]{ "client_secret" },
            //    ClientSecrets = new List<Secret> { new Secret("123456".Sha256())},
            //    AllowedScopes = new[] {"default-api"}
            //});
            return clients;
        }
    }
}
